package cn.hchaojie.camp.day12;

import org.junit.Test;

public class DemoThread2 {
	public static void main(String[] args) {
		Thread t1 = new Thread(new PrintString("我正在跑后台任务。。。", 100));
		Thread t2 = new Thread(new PrintNumber(100D));
		
		t1.start();
		t2.start();
	}
	
	// 调用test()方法的主线程，在t1,t2线程执行完之前，退出了，t1,t2 就会结束
	@Test
	public void test() throws InterruptedException {
		Thread t1 = new Thread(new PrintString("我正在跑后台任务。。。", 100));
		Thread t2 = new Thread(new PrintNumber(100D));
		
		t1.start();
		t2.start();
		
		t1.join();		// 让t1加入当前线程的执行流程，当前线程处于等待的状态
		t2.join();
		
		System.out.println("主线程结束。。。");
	}
}

class PrintString implements Runnable {
	private String message;
	private int count;
	
	public PrintString(String msg, int count) {
		this.message = msg;
		this.count = count;
	}

	@Override
	public void run() {
		try {
			while (count-- > 0) {
				Thread.sleep(50);
				System.out.println(this.message + " / " + count);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class PrintNumber implements Runnable {
	private Double num;
	public PrintNumber(Double num) {
		this.num = num;
	}

	@Override
	public void run() {
		try {
			while (this.num > 0) {
				Thread.sleep(100);
				
				System.out.println(this.num);
				this.num = this.num - 1;
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
}